// ChemeEqu.cpp : ���ļ����� "main" ����������ִ�н��ڴ˴���ʼ��������
//

#include "ChemObject.hpp"
#include "ChemUtils.hpp"
#include "ChemEquation.hpp"


int main()
{
    //Ca Cl2
    //Ca SO4
    //Fe2 (SO4)3

    //make Fe2
    Chemistry::Element Fe("Fe");
    Chemistry::ElementPair fe2Pair(Fe, 2);
    Chemistry::ElementCombine feCb;
    feCb.append(&fe2Pair);
    Chemistry::ElementCombinePair fePair(feCb,1);
    //make SO4
    Chemistry::Element S("S");
    Chemistry::Element O("O");
    Chemistry::ElementPair S1(S, 1);
    Chemistry::ElementPair O4(O, 4);
    Chemistry::ElementCombine SO4Cb;
    SO4Cb.append(&S1);
    SO4Cb.append(&O4);
    Chemistry::ElementCombinePair SO4CbPair4(SO4Cb, 3);

    //make Fe2SO43
    Chemistry::MolecularFormula Fe2SO43;
    Fe2SO43.append(&fePair);
    Fe2SO43.append(&SO4CbPair4);

    std::cout << Fe2SO43.getString() << std::endl;
    std::vector<std::string> els = Fe2SO43.getElements();
    for (size_t i = 0; i < els.size(); i++)
    {
        std::cout << els[i] << Fe2SO43.getElementCount(els[i].c_str()) << std::endl;
    }
	std::cout << std::endl;
    //make H2SO4
    Chemistry::Element H("H");
    Chemistry::ElementPair H2Pair(H, 2);
    Chemistry::ElementCombine H2PairCb;
    H2PairCb.append(&H2Pair);
    Chemistry::ElementCombinePair H2CbPair(H2PairCb, 1);
    Chemistry::MolecularFormula H2SO4;
    Chemistry::ElementCombinePair SO4CbPair1(SO4Cb, 1);
    H2SO4.append(&H2CbPair);
    H2SO4.append(&SO4CbPair1);
    std::cout << H2SO4.getString() << std::endl;
    std::vector<std::string> els2 = H2SO4.getElements();
    for (size_t i = 0; i < els2.size(); i++)
    {
        std::cout << els2[i] << H2SO4.getElementCount(els2[i].c_str()) << std::endl;
    }
    std::cout << std::endl;
    //make Fe2O3
	Chemistry::ElementPair O3(O, 3);
	Chemistry::ElementCombine O3Cb;
	O3Cb.append(&O3);
	Chemistry::ElementCombinePair O3CbPair(O3Cb, 1);
	Chemistry::MolecularFormula Fe2O3;
	Fe2O3.append(&fePair);
	Fe2O3.append(&O3CbPair);
	std::cout << Fe2O3.getString() << std::endl;
	std::vector<std::string> els3 = Fe2O3.getElements();
    for (size_t i = 0; i < els3.size(); i++)
    {
        std::cout << els3[i] << Fe2O3.getElementCount(els3[i].c_str()) << std::endl;
    }
    std::cout << std::endl;
    //Make H2O
    Chemistry::ElementPair OPair(O, 1);
    Chemistry::ElementCombine OPairCb;
    OPairCb.append(&OPair);
    Chemistry::ElementCombinePair OCbPair(OPairCb, 1);
    Chemistry::MolecularFormula H2O;
    H2O.append(&H2CbPair);
	H2O.append(&OCbPair);
	std::cout << H2O.getString() << std::endl;
	std::vector<std::string> els4 = H2O.getElements();
    for (size_t i = 0; i < els4.size(); i++)
    {
        std::cout << els4[i] << H2O.getElementCount(els4[i].c_str()) << std::endl;
    }
	// 测试化学方程式平衡功能
	// Fe2O3 + H2SO4 -> Fe2(SO4)3 + H2O
	std::cout << "\n=== 化学方程式平衡测试 ===" << std::endl;
	
	Chemistry::Equation equation;
	
	// 创建 Fe2O3
	Chemistry::EquationPosition leftPos1(&Fe2O3);
	equation.appendLeft(&leftPos1);
	
	// 创建 H2SO4  
	Chemistry::EquationPosition leftPos2(&H2SO4);
	equation.appendLeft(&leftPos2);
	
	// 创建 Fe2(SO4)3
	Chemistry::EquationPosition rightPos1(&Fe2SO43);
	equation.appendRight(&rightPos1);
	
	// 创建 H2O
	Chemistry::EquationPosition rightPos2(&H2O);
	equation.appendRight(&rightPos2);
	
	std::cout << "平衡前的方程式：" << std::endl;
	equation.print();
	
	if (equation.Balance()) {
		std::cout << "平衡成功！" << std::endl;
		std::cout << "平衡后的方程式：" << std::endl;
		equation.print();
	} else {
		std::cout << "平衡失败！" << std::endl;
	}
	
	return 0;
}

